package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.RoomBean;

import com.project.mapper.IFacilityMapper;
import com.project.mapper.IRoomMapper;
import com.project.mapper.IStudentMapper;
import com.project.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class RoomServiceImpl implements IRoomService {
    @Autowired
    private IRoomMapper roomMapper;
    @Autowired
    private IStudentMapper stuMapper;
    @Autowired
    private IFacilityMapper facMapper;
    @Override
    public IPage<RoomBean> findByItem(Integer pageNO, String status, String address, String type, String isLive) {
//        QueryWrapper<RoomBean> qw = new QueryWrapper<>();
//        if (status!=null&&status.length()!=0){
//            qw.like("r_status",status);
//        }
//        if (address!=null){
//            qw.like("r_address",address);
//        }
//        if (type!=null){
//            qw.like("r_type",type);
//        }

//        return roomMapper.selectPage(new Page(pageNO,5), qw);
        return roomMapper.findByItem(new Page(pageNO,5),address, status,type,isLive);
    }

    @Override
    public List<RoomBean> findBySex(String sex) {
        return roomMapper.findBySex(sex);
    }

    @Override
    public boolean delRoom(Integer roomId) {

        QueryWrapper qw = new QueryWrapper();
        qw.eq("fk_roomId",roomId);
        //统计房间人数
        Integer stuNum = stuMapper.selectCount(qw);
        if(stuNum != 0){
            return false;
        }
        facMapper.delete(qw);
        roomMapper.deleteById(roomId);

        return true;

    }

    @Override
    public RoomBean findById(Integer roomId) {

        RoomBean room= roomMapper.selectById(roomId);
        room.setStuList(stuMapper.findStuByRoomId(roomId));
        return room;
    }

    @Override
    public void addRoom(RoomBean room) {
        roomMapper.insert(room);
    }
}
